package com.leha.mp.core.Utils;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.ws.rs.core.MediaType;
import java.util.ResourceBundle;

/**
 * Created by justin on 15/8/17.
 */
public class SmsUtil {
    private static Logger logger = LogManager.getLogger(SmsUtil.class);
    public static final String API_KEY = ResourceBundle.getBundle("config").getString("sms.key");

    public static String send(String mobile ,String message){
        logger.info("send to "+mobile+" with message "+message);
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter("api","key-" + API_KEY));
        WebResource webResource = client.resource("http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", mobile);
        // 螺丝帽【普乐云康】
        formData.add("message", message+"【普乐云康】");
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        logger.debug("sms send:" + textEntity + " send status: "+status);
        return textEntity;
    }
}
